package com.jp.knowledge.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.internal.LinkedTreeMap;
import com.jiayen.dialog.LoadingDialog;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.adapter.HeadItemAdapter;
import com.jp.knowledge.adapter.OnItemListerner;
import com.jp.knowledge.db.DBHelper;
import com.jp.knowledge.fragment.TopicFragment;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.HeadLine;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.util.JsonUtil;
import com.jp.knowledge.util.ResultSubscriber;

import org.xutils.common.util.DensityUtil;

import java.util.ArrayList;
import java.util.List;

import rx.Subscriber;

/**
 * Created by jiayen on 16/10/12.
 * 删除原因dialog
 */

public class OpenTopicDialog extends Dialog implements View.OnClickListener, ResultSubscriber.OnResultListener {

    private  Context context;
    private RecyclerView recyclerView;
    private List<HeadLine> list;
    private HeadItemAdapter adapter;
    private TextView openBtn;
    private String infoId;
    private View rootView;
    private LoadingDialog loading;
    private ResultSubscriber.OnResultListener  resultListenre;//开启话题成功后回调
    public OpenTopicDialog(Context context) {
        super(context,R.style.custom_idalgo);
        this.context=context;
        loading = new LoadingDialog(context);
    }

    public void setInfoId(String infoId) {
        this.infoId = infoId;
    }

    public void setResultListenre(ResultSubscriber.OnResultListener resultListenre) {
        this.resultListenre = resultListenre;
    }

    public OpenTopicDialog(Context context, int themeResId) {
        super(context, themeResId);
        this.context=context;
    }

    protected OpenTopicDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.context=context;
    }

    public HeadLine getItem(int postion){
        return list==null?null:list.get(postion);
    }



    public void update(int postion){
        if(list==null)return;
        HeadLine headLine = list.get(postion);
        headLine.setCheck(!headLine.isCheck());
        adapter.update(list);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        rootView = getLayoutInflater().inflate(R.layout.open_topic_dialog_layout,null);
        setContentView(rootView);
        Window dialogWindow = this.getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width= WindowManager.LayoutParams.MATCH_PARENT;
//        lp.alpha = 0.8f;

        lp.height= DensityUtil.getScreenHeight()/5*2;
        dialogWindow.setAttributes(lp);
        init();
    }

    private void init(){
        recyclerView = (RecyclerView) findViewById(R.id.head_view);
        recyclerView.setHasFixedSize(true);
        openBtn = (TextView) findViewById(R.id.open_btn);
        DBHelper.getInstance().findAll(HeadLine.class, new Subscriber<List<HeadLine>>() {
            @Override
            public void onCompleted() {
//                ToasUtil.toast(context, "onCompleted");
            }
            @Override
            public void onError(Throwable e) {
                ToasUtil.toast(context, "onError");
            }

            @Override
            public void onNext(List<HeadLine> headLines) {
                list = headLines;
                if (headLines != null && headLines.size() > 0) {
                    bindViewData();
                }
            }
        });


//       getTags();
    }

    private void getTags() {
        JsonObject param = new JsonObject();
        param.addProperty("id",infoId);
        HttpHelper.getInstance(context).topicTags(param,1,this);
    }

    private void bindViewData() {
        adapter = new HeadItemAdapter(context,list);
        GridLayoutManager layoutMannager = new GridLayoutManager(context, 4, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutMannager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(adapter);
        adapter.setOpenTopic(true);
        adapter.setItemListerner(new OnItemListerner() {
            @Override
            public void onItemClick(View view, int position) {
                update(position);
            }
        });
        openBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case  R.id.open_btn://开启话题
                    openTopic();
                break;
            default:
                break;
        }
    }


    /**
     * 开启话题
     */
    private void openTopic() {
        JsonObject param = new JsonObject();
        param.addProperty("infoId",infoId);
        param.add("types",getTypesParam());
        HttpHelper.getInstance(context).addTopic(param, TopicFragment.OPEN_TOPIC,resultListenre);
    }
    /**
     * 获取选中的分类
     * @return
     */
    public JsonArray getTypesParam(){
        JsonArray array = new JsonArray();
        for (HeadLine headLine:list) {
            if(headLine.isCheck()){
               JsonObject obj = new JsonObject();
                obj.addProperty("id",headLine.getTypeId());
                obj.addProperty("name",headLine.getTypeName());
                array.add(obj);
            }
        }
        return array;
    }

    @Override
    public void onStart(int requestType) {
        loading.show();
    }

    @Override
    public void onCompleted(int requestType) {
        loading.cancel();
    }

    @Override
    public void onError(int requestType) {
        loading.cancel();
    }

    @Override
    public void onNext(IModel data, int requestType) {
        int code = data.getErrcode();
        if(code==0){
            list = JsonUtil.getInstance().listMapToObject((ArrayList<LinkedTreeMap<String, Object>>) data.getData(),HeadLine.class);
            if (list != null && list.size() > 0) {
                bindViewData();
            }
        }else{
            ToasUtil.toast(context,"获取数据失败");
        }
    }
}
